﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RMS.Utils.MVP;
using RMS.Web.Shell.Presenter;
using RMS.Web.Shell.View;
using RMS.DataAccess.Entities;
using RMS.Utils.Navigator;

namespace RMS
{
    public partial class SearchInPatientView : Page<SearchInPatientPresenter>, ISearchInPatientView
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            FillPationsGriadView(0, Pager.RecordsPerPage);
        }

        protected void gvPatients_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            GridView gv = (GridView)sender;
            int patientId = Convert.ToInt32(gv.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
            ButtonAction(e.CommandName, patientId);
        }

        private void ButtonAction(string command, int patientId)
        {
            if (command == "Edit")
            {
                Navigator.NavigateTo(Pages.WebPagesReception_PatientView, new string[] { "PID=" + patientId.ToString().Encrypt(Constants.ApplicationKey) });
            }
            else if (command == "Remove")
            {
                this.Presenter.deletePatient(patientId);
            }
            else if (command == "GetRequests")
            {
                Navigator.NavigateTo(Pages.WebPagesReception_SearchInBookedRadiologyView, new string[] { patientId.ToString().Encrypt(Constants.ApplicationKey) });
            }
        }

        protected void Pager_PageIndexChange(object sender, EventArgs e)
        {
            FillPationsGriadView(Pager.CurrentStartRow, Pager.RecordsPerPage);
        }

        private void FillPationsGriadView(int StartIndex, int Lenght)
        {
            if (txtPationID.Text != "")
                gvPatients.DataSource = this.Presenter.GetPationt(Convert.ToInt32(txtPationID.Text));
            else
            {
                var result = this.Presenter.GetPationt(txtPationtName.Text, StartIndex, Pager.RecordsPerPage);
                gvPatients.DataSource = result.Rows;
                Pager.Visible = true;
                Pager.TotalRecords = 100;
            }
            gvPatients.DataBind();

        }


    }
}